package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.config.satoken.SaTokenUtilPy;
import com.ls.controller.activity.model.AddActivityModel;
import com.ls.controller.activity.model.EditActivityModel;
import com.ls.controller.activity.model.updateStatusModel;
import com.ls.entity.Activity;
import com.ls.entity.ActivityUser;
import com.ls.entity.Customer;
import com.ls.mapper.ActivityMapper;
import com.ls.service.IActivityService;
import com.ls.service.IActivityUserService;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.ls.constant.SystemConstants.NUMBER_1;
import static com.ls.constant.SystemConstants.VALID_TRUE;

@Service
@Slf4j
@RequiredArgsConstructor
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {
    private final IActivityUserService activityUserService;
    /** 活动列表 */
    @Override
    public ResultData pageList() {
        List<Activity> list = list(new LambdaQueryWrapper<Activity>().orderByDesc(Activity::getCreateTime));
        list.forEach(item -> {
            item.setAuName(activityUserService.getById(item.getAuId()).getAuName());
        });
        return ResultData.successData(list);
    }
    /** 新增活动 */
    @Override
    public ResultData add(AddActivityModel model) {
        Activity activity = new Activity();
        BeanUtils.copyProperties(model, activity);
        activity.setValidFlag(VALID_TRUE);
        return save(activity) ?
                ResultData.successMsg("新增成功") :
                ResultData.failure("新增失败");
    }
    /** 编辑活动 */
    @Override
    public ResultData edit(EditActivityModel model) {
        Activity activity = getById(model.getActivityId());
        if (activity == null){
            return ResultData.failure("活动对象不存在");
        }
        BeanUtils.copyProperties(model, activity);
        activity.setUpdateTime(LocalDateTime.now());
        return updateById(activity) ?
                ResultData.successMsg("修改成功") :
                ResultData.failure("修改失败");
    }
    /** 修改活动启用状态 */
    @Override
    public ResultData updateStatus(updateStatusModel model) {
        Activity activity = getById(model.getActivityId());
        if (activity == null){
            return ResultData.failure("活动对象不存在");
        }
        BeanUtils.copyProperties(model, activity);
        activity.setUpdateTime(LocalDateTime.now());
        return updateById(activity) ?
                ResultData.successMsg("修改成功") :
                ResultData.failure("修改失败");
    }
    /**app展示活动活动*/
    @Override
    public ResultData getActivityList() {
        Customer customer = SaTokenUtilPy.getCustomer();
        List<Activity> list = list(new LambdaQueryWrapper<Activity>()
                .eq(Activity::getValidFlag,VALID_TRUE)
                .orderByDesc(Activity::getCreateTime));
        List<Activity> activityList=new ArrayList<>();
        for (Activity activity : list) {
            //所有用户
            if (activity.getAuId()==1){
                activityList.add(activity);
            }
            //会员
            if (activity.getAuId()==2&&customer.getMemberStatus()==1){
                activityList.add(activity);
            }
            //非会员
            if (activity.getAuId()==3&&customer.getMemberStatus()!=1){
                activityList.add(activity);
            }
        }
        activityList.forEach(item -> {
            item.setAuName(activityUserService.getById(item.getAuId()).getAuName());
        });
        return ResultData.successData(activityList);
    }
}
